//
//  ZZHomePictureView.swift
//  web
//
//  Created by 张卓 on 2017/2/5.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit
import SDWebImage

//计算每张图片的高
let itemH = (ZZSCREENW - 2*10 - 2*5) / 3
let pictrueCellID = "pictrueCellID"
class ZZHomePictureView: UICollectionView {
    
    //定义一个属性供外界赋值
    var pic_urls: [ZZPictureModel]?{
        didSet{
            //获取配图的size
            let size = dealPictureViewSize(count: pic_urls?.count ?? 0)
            //设置约束
            snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            //获取到的layout
            let layoutS = collectionViewLayout as! UICollectionViewFlowLayout
            if pic_urls?.count == 1 {
                layoutS.itemSize = size
            }else {
                layoutS.itemSize = CGSize(width: itemH, height: itemH)
            }
            
            //刷新UI
            reloadData()
            
        
        }
    }

    func dealPictureViewSize(count: Int)-> CGSize{
        //判断是否为单张图片
        if count == 1 {
            //判断地址是否为nil
            if let picUrlString = pic_urls?.first?.thumbnail_pic {
                //在本地拿到我们下载下来的图片
                if let img = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picUrlString){
                    //得到本地图片的大小
                    var w = img.size.width
                    var h = img.size.height
                    //如果宽度小于80  默认就为80
                    if w < 80 {
                        w = 80
                    }
                    //如果高度大于150 默认为150
                    if h > 150 {
                        h = 150
                    }
                    //最终的大小
                    return CGSize(width: w, height: h)
                }
            }
        }
        //通过图片张数来计算 行数 和 列数
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1) / 3+1
        //计算配图的 宽 和 高
        let h = CGFloat(row)*itemH + 5*CGFloat(row-1)
        let w = CGFloat(col)*itemH + 5*CGFloat(col-1)
        return CGSize(width: w, height: h)
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //自定义layout
        let layout = UICollectionViewFlowLayout()
        //设置item的宽高
        layout.itemSize = CGSize(width: itemH, height: itemH)
        //设置间距
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置相关视图
    private func setupUI(){
        backgroundColor = ZZRandomColor()
        //设置数据源代理
        dataSource = self
        //注册机制
        register(ZZHomePictureViewCell.self, forCellWithReuseIdentifier: pictrueCellID)
    }
}

extension ZZHomePictureView: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: pictrueCellID, for: indexPath) as! ZZHomePictureViewCell
        cell.pic_url = pic_urls![indexPath.item]
        
        return cell
    }
    
}

class ZZHomePictureViewCell: UICollectionViewCell {
    //定义一个属性供外界赋值
    var pic_url : ZZPictureModel?{
        didSet{
            bgImgView.zz_setImage(urlString: pic_url?.thumbnail_pic)
            //判断thumbnail 后缀是否是gif
            if let pic = pic_url?.thumbnail_pic,pic.hasSuffix("gif"){
                gifImgView.isHidden = false
            }else{
                gifImgView.isHidden = true
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置视图
    private func setupUI(){
        //添加控件
        contentView.addSubview(bgImgView)
        contentView.addSubview(gifImgView)
        //添加约束
        bgImgView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        gifImgView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(contentView)
        }
    }
    //懒加载图片
    private lazy var bgImgView :UIImageView = {
        let img = UIImageView(imgName: "avatar_default_big")
        //设置图片的填充模式
        img.contentMode = .scaleAspectFill
        //多余的部分裁剪调
        img.clipsToBounds = true
        return img
    }()
    // gif 图片标示
    private lazy var gifImgView : UIImageView = UIImageView(imgName: "timeline_image_gif")
}


